package com.kevis.zhunblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevis.zhunblog.core.dto.sys.SysMenu;
import com.kevis.zhunblog.core.entities.PowerMenuEntity;
import com.kevis.zhunblog.core.entities.RolePowerEntity;
import com.kevis.zhunblog.mapper.PowerMenuMapper;
import com.kevis.zhunblog.mapper.RolePowerMapper;
import com.kevis.zhunblog.service.RolePowerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class RolePowerServiceImpl extends ServiceImpl<RolePowerMapper, RolePowerEntity>
        implements RolePowerService {

    @Resource
    private PowerMenuMapper powerMenuMapper;

    @Resource
    private RolePowerMapper rolePowerMapper;

    @Override
    public List<SysMenu> getUserMenu(int roleId) {
        //未传入角色
        if (roleId <= 0) return null;

        List<SysMenu> result = new ArrayList<SysMenu>();
        LambdaQueryWrapper<PowerMenuEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PowerMenuEntity::getStatus, 0);
        lambdaQueryWrapper.eq(PowerMenuEntity::getIsShow, 1);
        //非超管的需要去取一下对应的菜单集合
        if (roleId != 9999) {
            LambdaQueryChainWrapper<RolePowerEntity> wrapper = lambdaQuery().select(RolePowerEntity::getPowerId);
            wrapper.eq(RolePowerEntity::getRoleId, roleId);
            List<RolePowerEntity> rolesList = wrapper.list();
            //这个角色没有对应菜单
            if (rolesList == null || rolesList.size() == 0) return null;
            Set<Integer> setPowerId = rolesList.stream().map(RolePowerEntity::getPowerId).collect(Collectors.toSet());
//        Object[] objects = rolesList.stream().map(RolePowerEntity::getPowerId).toArray();
//        List<Integer> collect2 = rolesList.stream().map(RolePowerEntity::getPowerId).collect(Collectors.toList());
            //菜单表的id在角色对应的菜单集合里
            lambdaQueryWrapper.in(PowerMenuEntity::getId, setPowerId);
        }

        List<PowerMenuEntity> list = powerMenuMapper.selectList(lambdaQueryWrapper);
        if (list != null && list.size() > 0) {
            result = list.stream().map(p -> {
                SysMenu menu = new SysMenu();
                menu.setHref(p.getUrl());
                menu.setIcon(p.getIcon());
                menu.setId(p.getId());
                menu.setGuid(p.getGuid());
                menu.setOpenType(p.getOpenType());
                menu.setParentId(p.getParentId());
                menu.setTitle(p.getTitle());
                menu.setType(p.getMenuType());
                //result.add(menu);
                return menu;
            }).collect(Collectors.toList());
        }
        return result;
    }

    @Override
    public Set<Integer> getRolesByUrlMenuCode(String url, String menuCode) {
        return rolePowerMapper.getRolesByUrlMenuCode(url, menuCode);
    }

    @Override
    public Set<Integer> getRoleByPowerId(Integer powerId) {
        return rolePowerMapper.getRoleByPowerId(powerId);
    }
}




